(function($) {
	if(typeof(BootstrapDialog)=="undefined"){
		return;
	}
	BootstrapDialog.defaultOptions.draggable = true;
	BootstrapDialog.prototype.updateTabindex = function () {
		//not set tab index avoid select2 not focus issue
		return this;
	}
	BootstrapDialog.prototype.updateZIndex = function () {
        if (this.isOpened()) {
            var zIndexBackdrop = 10040;
            var zIndexModal = 10050;
            var dialogCount = 0;
            $.each(BootstrapDialog.dialogs, function (dialogId, dialogInstance) {
                if (dialogInstance.isRealized() && dialogInstance.isOpened()) {
                    dialogCount++;
                }
            });
            var $modal = this.getModal();
            var $backdrop = $modal.data('bs.modal').$backdrop;
            if(this.options.remote){
            	$modal.data('z-index', zIndexModal + (dialogCount - 1) * 20);
            } else {
            	$modal.css('z-index', zIndexModal + (dialogCount - 1) * 20);
            }
            $backdrop.css('z-index', zIndexBackdrop + (dialogCount - 1) * 20);
        }

        return this;
    }
	$.mms.alert = function() {
		var alertOptions = {};
        var defaultAlertOptions = {
        	size: BootstrapDialog.SIZE_SMALL,
            type: "type-danger",
            title : $.i18n.alert,
            message : $.i18n.illegalOperation,
            closable: true,
            draggable: true,
            buttonLabel: $.i18n.close
        };
		if (typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
            alertOptions = $.extend(true, defaultAlertOptions, arguments[0]);
        } else {
            alertOptions = $.extend(true, defaultAlertOptions, {
                message: arguments[0],
                callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
            });
        }
        return BootstrapDialog.alert(alertOptions);
    };
    
    $.mms.confirm = function() {
    	var confirmOptions = {};
        var defaultConfirmOptions = {
        	size: BootstrapDialog.SIZE_SMALL,
            type: "type-primary",
            title : $.i18n.confirm,
            message : $.i18n.confirmToOperate,
            closable: true,
            draggable: true,
            btnCancelLabel: $.i18n.cancel,
            btnCancelClass: null,
            btnOKLabel: $.i18n.ok,
            btnOKClass: null,
            btnsOrder: BootstrapDialog.defaultOptions.btnsOrder,
            ok: $.noop, //自定义确认回调
            cancel: $.noop //自定义取消回调,
        };
        if (typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
            confirmOptions = $.extend(true, defaultConfirmOptions, arguments[0]);
        } else {
            confirmOptions = $.extend(true, defaultConfirmOptions, {
                message: arguments[0],
                callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
            });
        }
        if (!confirmOptions.callback) {
        	confirmOptions.callback = function(result) {
        		if (result) {
        			return confirmOptions.ok();
                } else {
                	return confirmOptions.cancel();
                }
        	}
        }
        return BootstrapDialog.confirm(confirmOptions);
    };
    
    $.mms.success = function(message, callback){
    	return $.mms.alert({
    		title : $.i18n.success,
    		type: "type-success",
    		message: message,
    		callback: callback
    	});
    };
    
    $.mms.loading = function(options){
    	var loadingTmpl = '<div class="progress progress-striped active">'+
		    '<div class="progress-bar progress-bar-info" role="progressbar" style="width: 100%">'+
			'<span class="sr-only"> 100% Complete </span>'+
		    '</div>'+
		'</div>';
    	var dialog = new BootstrapDialog({
            message: function(dialogRef){
                return loadingTmpl;
            },
            closable: false
        });
        dialog.realize();
        dialog.getModalHeader().hide();
        dialog.getModalFooter().hide();
        dialog.getModalContent().removeClass('modal-content');
        dialog.getModal().css('overflow-y','hidden');
        //dialog.getModal().css('overflow-x','hidden').css('overflow-y','hidden');
        dialog.setSize(BootstrapDialog.SIZE_SMALL);
        dialog.open();
    }
    $.mms.dialog = function(options){
        var defaultDialogOptions = {
        };
        var alignCenter = options.alignCenter;
        if(options.remote){
        	var loadingTmpl = '<div class="progress progress-striped active">'+
	    	    '<div class="progress-bar progress-bar-info" role="progressbar" style="width: 100%">'+
	    		'<span class="sr-only"> 100% Complete </span>'+
	    	    '</div>'+
	    	'</div>';
        	var defaultRemoteOptions = {
        		onloading: $.noop,
        		onloaded: $.noop,
        		processData: function(dialogRef, data){
        			return data;
        		}
        	}
        	
        	var remoteOptions = $.extend(true, defaultRemoteOptions, options.remote);
        	defaultDialogOptions = {
        		nl2br: false,
        		message: loadingTmpl,
        		onhidden: function(dialogRef){
        			$("body").removeClass("page-overflow").removeClass("modal-open-noscroll");
        			dialogRef.getModal().parent(".modal-scrollable").remove();
        			if(options.onhidden){
        				options.onhidden(dialogRef);
        			}
                },
                onshow: function(dialogRef){
                	dialogRef.getModal().data("align-top", true);
                	dialogRef.getModalFooter().hide();
                },
    			onshown: function(dialogRef){
    				$.ajax($.extend(true, {
    					cache : false,
    					headers : {
    						"X-PJAX": "true"
    					},
    					success : function(data, textStatus, jqXHR){
    						remoteOptions.onloading(dialogRef, data);
    						dialogRef.setMessage(remoteOptions.processData(dialogRef, data));
    						
    						var $modal = dialogRef.getModal();
    						var $wrapper = $('<div class="modal-scrollable"></div>').css("z-index", $modal.data("z-index"));
    						$modal.wrap($wrapper).addClass("modal-overflow");
    						
    						$("body").addClass("page-overflow").addClass("modal-open-noscroll")
    						remoteOptions.onloaded(dialogRef, data);
    						dialogRef.getModalFooter().show();
    					}
    				}, remoteOptions.ajax));
                },
        	}
        }
        var dialogOptions = $.extend(true, defaultDialogOptions, options);
        return new BootstrapDialog(dialogOptions).open();
    };
    
    $.mms.dialog2 = function(options){
        //App.blockUI("加载中");
        if(!options.remote){
        	var dialogOptions = $.extend(true, {}, options);
        	var dialog = new BootstrapDialog(dialogOptions);
        	dialog.realize();
        	return dialog.open();
        }
        
        var defaultDialogOptions = {
			nl2br: false,
			onhidden: function(dialog){
				$("body").removeClass("page-overflow").removeClass("modal-open-noscroll");
				dialog.getModal().parent(".modal-scrollable").remove();
				if(options.onhidden){
					options.onhidden(dialog);
				}
	        },
	        remote: {
	        	onloading: $.noop,
	    		onloaded: $.noop,
	    		processData: function(dialogRef, data){
	    			return data;
	    		}
	        }
		}
        var dialogOptions = $.extend(true, defaultDialogOptions, options);
        var dialog = new BootstrapDialog(dialogOptions);
    	dialog.realize();
        // do remote
        
    	var remoteOptions = dialogOptions.remote;
    	$.ajax($.extend(true, {
			cache : false,
			headers : {
				"X-PJAX": "true"
			},
			success : function(data, textStatus, jqXHR){
				App.unblockUI();
				remoteOptions.onloading(dialog, data);
				dialog.setMessage(remoteOptions.processData(dialog, data));
				//adjustDialog(dialog.getModalDialog());
				
				var $modal = dialog.getModal();
				var $wrapper = $('<div class="modal-scrollable"></div>').css("z-index", $modal.data("z-index"));
				$modal.wrap($wrapper).addClass("modal-overflow");
				
				$("body").addClass("page-overflow").addClass("modal-open-noscroll")
				remoteOptions.onloaded(dialog, data);
				dialog.open();
			}
		}, remoteOptions.ajax));
        return dialog;
    };
    
    
})(jQuery);


jQuery(function($){

});
